<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择项目名" filterable>
          <el-option v-for="item in projectList" :key="item.projectId" :label="item.projectName"
            :value="item.projectId" />
        </el-select>
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-date-picker v-model="queryParams.year" value-format="yyyy" type="year" placeholder="年份" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="创建人" align="center" prop="nickName" />
      <el-table-column label="报告名称" align="center" prop="name" />
      <el-table-column label="委托单位名" align="center" prop="customerName" />
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == '1'" type="warning">待签字</el-tag>
          <el-tag v-if="scope.row.status == '2'" type="info">待审核</el-tag>
          <el-tag v-if="scope.row.status == '3'">审核通过</el-tag>
          <el-tag v-if="scope.row.status == '4'" type="danger">审核拒绝</el-tag>
          <el-tag v-if="scope.row.status == '5'" type="success">业主审核通过</el-tag>
          <el-tag v-if="scope.row.status == '6'" type="danger">业主审核拒绝</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="拒绝原因" align="center" prop="reasonForRefusal" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == '1' && scope.row.qualifiedForReview == 1" style="margin: 0 6px"
            @click="passStatus(scope.row)">通过</el-tag>
          <el-tag v-if="scope.row.status == '1' && scope.row.qualifiedForReview == 1" type="danger"
            @click="refuseStatus(scope.row)">拒绝</el-tag>
          <el-button size="mini" type="text" icon="el-icon-download" @click="hnadleDownload(scope.row)"
            v-if="scope.row.status != '1'">下载</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row.id)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- @refreshList="refreshList" -->
    <Detail v-if="detailShow" ref="Detail" />
  </div>
</template>

<script>
import { listReport, reportReview } from "@/api/system/report";
import { getListByCustomerId } from "@/api/plan-produce/task";
import Detail from "./detail.vue";
export default {
  name: "Report",
  components: { Detail },
  data() {
    return {
      loading: false, // 遮罩层
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      dataList: [], // 维保报告表格数据
      detailShow: false, // 是否显示弹出层
      projectList: [], // 项目列表
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        projectId: null,
        year: null, // 查询年份
        startTime: null,
        endTime: null,
      },
    };
  },
  created() {
    this.queryParams.customerId = this.$route.query.customerId;
    this.getProjectList();
    this.getList();
  },
  methods: {
    passStatus(row) {
      this.$confirm("确定审批通过?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          reportReview({
            id: row.id,
            status: 2,
          }).then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess(res.msg);
              this.getList();
            }
          });
        })
        .catch(() => { });
    },
    refuseStatus(row) {
      this.$prompt("请输入理由", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          reportReview({
            id: row.id,
            status: 3,
            reasonForRefusal: value,
          }).then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess(res.msg);
              this.getList();
            }
          });
        })
        .catch(() => { });
    },
    // 获取项目列表
    getProjectList() {
      getListByCustomerId(this.queryParams.customerId).then((res) => {
        this.projectList = res.data;
      });
    },
    /** 查询维保报告列表 */
    getList() {
      this.loading = true;
      listReport(this.queryParams)
        .then((res) => {
          this.dataList = res.data.records;
          this.total = res.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 打开详情弹窗
    handleDetail(id) {
      this.detailShow = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(id);
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.year) {
        this.queryParams.startTime = this.queryParams.year + "-01-01 00:00:00";
        this.queryParams.endTime = this.queryParams.year + "-12-31 23:59:59";
      } else {
        this.queryParams.startTime = "";
        this.queryParams.endTime = "";
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      console.log(this.queryParams);
      this.handleQuery();
    },
    /** 下载按钮操作 */
    hnadleDownload(row) {
      this.$download.resource(row.reportPdfPath);
    },
  },
};
</script>
